﻿using UnityEngine;

public class PlayerController : MonoBehaviour {

    [SerializeField]
    private float speed;
    [SerializeField]
    private float height;

    private bool isJump = true;

    Animator playerAniState;
    Rigidbody2D rigid;
    public static PlayerController controller;
    CapsuleCollider2D collider;
    CharacterController char_controller;

    public GameManager gameManager;
    UIManager uiManager;

    private void Awake()
    {
        controller = this;
    }

    private float _gravityTemp;
    // Use this for initialization
    void Start () {
        playerAniState = GetComponent<Animator>();
        rigid = GetComponent<Rigidbody2D>();
        //controller = GetComponent<PlayerController>();
        collider = GetComponent<CapsuleCollider2D>();

        uiManager = gameManager.GetComponent<UIManager>();
    }
	
	// Update is called once per frame
	void Update () {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        transform.Translate(new Vector2(horizontal * speed * Time.deltaTime, 0));

        if(isJump)
        {
            if(Input.GetKeyDown(KeyCode.Space))
            {
                playerAniState.SetBool("isJump", true);
                rigid.AddForce(new Vector2(0, height * 100));
            } 
        }

        if(horizontal < 0)
        {
            playerAniState.SetBool("isMove", true);
            GetComponent<SpriteRenderer>().flipX = true;
        }
        else if(horizontal > 0)
        {
            playerAniState.SetBool("isMove", true);
            GetComponent<SpriteRenderer>().flipX = false;
        }
        else
        {
            playerAniState.SetBool("isMove", false);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        GameObject colOB = collision.gameObject;
        if (colOB.tag.Equals("Ground"))
        {
            playerAniState.SetBool("isJump", false);
            isJump = true;
        }

        if (colOB.tag.Equals("Enemy") || colOB.name.Equals("DeadLine"))
        {
            Debug.Log("游戏结束");
            controller.enabled = false;
            playerAniState.SetBool("isDead", true);
            rigid.AddForce(new Vector2(0, height * 100));
            collider.isTrigger = true;
        }

        if (colOB.name.Equals("jumpToucArea"))
        {
            Debug.Log("踩踏");
            rigid.AddForce(new Vector2(0, height * 100));

            GameObject effect = Resources.Load("DestroyEffect") as GameObject;
            effect.transform.position = colOB.transform.position;
            Destroy(colOB.GetComponentInParent<SpriteRenderer>().gameObject);
        }

        if (colOB.name.Equals("FinalPoint"))
        {
            Debug.Log("游戏胜利");

            uiManager.EndGame("胜利");
            gameManager.isPass = true;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject ob = collision.gameObject;
        if(ob.tag.Equals("InfoPoint"))
        {
            Debug.Log("展示信息");
            Sign _sign = ob.GetComponent<Sign>();
            uiManager.Pop(_sign.content);
            if(!_sign.isPassed)
            {
                _sign.isPassed = true;
                
            }
        }

        if (ob.name.Equals("DeadLine"))
        {
            uiManager.EndGame("失败");
            gameManager.isPass = false;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag.Equals("InfoPoint"))
        {
            uiManager.ExitPop();
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag.Equals("Ground"))
        {
            isJump = false;
        }
    }
}
